//
//  MOMyOrderFoodViewController.m
//  MobileOffice
//
//  Created by 晓 on 2019/10/15.
//  Copyright © 2019 com.idolmedia. All rights reserved.
//

#import "MOMyOrderFoodViewController.h"
#import "MOCommonUseModel.h"
#import "MOCommonUseViewCell.h"
#import "MOFoodOrderListViewController.h"
#import "MOFoodAppraiseViewController.h"
#import "MODingCanViewController.h"
#import "MOFoodZhanDanViewController.h"
#import "MOCaiPuViewController.h"

@interface MOMyOrderFoodViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property(nonatomic,strong)UICollectionView *collectionView;

@property(nonatomic,strong)NSMutableArray *dataArray;

@property(nonatomic,strong)UIButton *myMoneyButton;

@end

@implementation MOMyOrderFoodViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initNav];
    [self initView];
    [self requestData];
}

- (void)initView{
    self.title = @"我的订餐";
    [self.view addSubview:self.collectionView];
}

-(void)initNav
{
    self.myMoneyButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 80, 30)];
    [self.myMoneyButton setTitle:@"" forState:UIControlStateNormal];
    self.myMoneyButton.titleLabel.font = FONT_SIZE(13);
    [self.myMoneyButton setContentHorizontalAlignment:(UIControlContentHorizontalAlignmentRight)];
    [self.myMoneyButton setTitleColor:KBlueColor_2C94FF forState:UIControlStateNormal];
    [self.myMoneyButton addTarget:self action:@selector(clickMyMoney) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:self.myMoneyButton];
    
}

-(void)clickMyMoney
{
    MOFoodZhanDanViewController *vc = [[MOFoodZhanDanViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)requestData
{
    self.dataArray = [NSMutableArray array];
    NSArray *nameArr = @[@"订餐",@"我的订单",@"菜品评价",@"菜谱",@"",@""];;
    NSArray *imageArr = @[@"diingcan_main",@"dingcan_dingdan",@"dingcan_pingjia",@"caipu",@"",@""];
    for (int i = 0; i<nameArr.count; i++) {
        MOCommonUseModel *model = [[MOCommonUseModel alloc]init];
        model.nameStr = nameArr[i];
        model.imageStr = imageArr[i];
        model.itemIndex = i;
        model.typeStr = @"0";
        [self.dataArray addObject:model];
    }
    [self.collectionView reloadData];
    
    [self getMyPrice];
    
}

-(void)getMyPrice{
    //余额
    NSString *postUrl = [NSString stringWithFormat:@"%@?authorization=%@",USERACCOUNT,[NSString URLEncodedString:[MOUser sharedMOUser].token]];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSDictionary *dic = @{
                          @"userId":[MOUser sharedMOUser].userName
                          };
    [PPNetworkHelper POST:postUrl parameters:dic success:^(id responseObject) {
        NSLog(@"余额：%@",responseObject);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if ([responseObject isKindOfClass:[NSString class]]) {
            NSString *m = [self removeFloatAllZero:responseObject];
            [self.myMoneyButton setTitle:[NSString stringWithFormat:@"¥ %@",m] forState:UIControlStateNormal];
        }
        
    } failure:^(NSError *error) {
        NSLog(@"余额：%@",[error localizedDescription]);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}

-(NSString*)removeFloatAllZero:(NSString*)string
{
    NSString * testNumber = string;
    NSString * outNumber = [NSString stringWithFormat:@"%@",@(testNumber.floatValue)];
    return outNumber;
}

#pragma mark -UICollectionViewDataSource,UICollectionViewDelegate
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    MOCommonUseViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.model = self.dataArray[indexPath.item];
    return cell;
    
}
//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake(SCREEN_WIDTH/3, SCREEN_WIDTH/3);
    
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    
    return UIEdgeInsetsMake(0, 0, 0, 0);
    
}

//定义每个UICollectionView 横向的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    
    return 0;
}

//定义每个UICollectionView 竖向的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    
    return 0;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    
    return CGSizeMake(0,0);
    
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.item==0) {
        MODingCanViewController *vc = [[MODingCanViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
        
    }else if (indexPath.item==1){
        MOFoodOrderListViewController *FoodOrderListVc = [[MOFoodOrderListViewController alloc] init];
        [self.navigationController pushViewController:FoodOrderListVc animated:YES];
        
    }else if (indexPath.item==2){
        MOFoodAppraiseViewController *foodAppraiseVC = [[MOFoodAppraiseViewController alloc] init];
        [self.navigationController pushViewController:foodAppraiseVC animated:YES];
        
    }else if (indexPath.item==3){
        
        MOCaiPuViewController *caiPuVC = [[MOCaiPuViewController alloc] init];
        [self.navigationController pushViewController:caiPuVC animated:YES];
    }
}


#pragma mark -懒加载
-(UICollectionView *)collectionView
{
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT-StatusBarAndNavigationBarHeight) collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.alwaysBounceVertical = YES;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        
        [_collectionView registerClass:[MOCommonUseViewCell class] forCellWithReuseIdentifier:@"cell"];
        
    }
    return _collectionView;
}


@end
